package com.zut.base.common.util;

import cn.hutool.core.util.StrUtil;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 常用工具类
 *
 * @author zhang
 * @date 2020-11-10 12:20
 */
public class CommonUtil {

    /**
     * list数据转换
     *
     * @param list list对象
     * @param func lamdba 表达式 function
     * @param <E>  原对象
     * @param <T>  转换完的对象
     * @return List<E>
     */
    public static <E, T> List<E> convers(List<T> list, Function<T, E> func) {
        return list.stream().collect(ArrayList::new, (li, p) -> li.add(func.apply(p)), List::addAll);
    }


    /**
     * 异常捕获
     *
     * @param resolver resolver
     * @param <T>      T
     * @return
     */
    public static <T> Optional<T> resolve(Supplier<T> resolver) {
        Optional<T> optional = Optional.empty();
        try {
            T result = resolver.get();
            optional = Optional.ofNullable(result);
        } catch (Exception e) {
            optional = Optional.empty();
        }
        return optional;
    }

    public static Optional<String> emptyStr(String string) {
        return Optional.ofNullable(string).filter(StrUtil::isNotBlank);
    }

    public static <E> void Sort(List<E> list, final String field, final String sort) {
        Collections.sort(list, new Comparator() {
            public int compare(Object a, Object b) {
                int ret = 0;
                try {
                    Field f1 = ((E) a).getClass().getDeclaredField(field);
                    Field f2 = ((E) b).getClass().getDeclaredField(field);
                    f1.setAccessible(true);
                    f2.setAccessible(true);
                    if (sort != null && "desc".equals(sort))// 倒序
                        ret = f2.get(b).toString().compareTo(f1.get(a).toString());
                    else
                        // 正序
                        ret = f1.get(a).toString()
                                .compareTo(f2.get(b).toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return ret;
            }
        });
    }

    public static String dataToUpper(String dateString) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(dateTime);
        int year = ca.get(Calendar.YEAR);
        int month = ca.get(Calendar.MONTH) + 1;
        int day = ca.get(Calendar.DAY_OF_MONTH);
        return numToUpper(year) + "年" + monthToUppder(month) + "月" + dayToUppder(day) + "日";
    }

    // 将数字转化为大写（字体格式可自己定义）
    public static String numToUpper(int num) {
        //String u[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        String u[] = {"〇","一","二","三","四","五","六","七","八","九"};
        char[] str = String.valueOf(num).toCharArray();
        String rstr = "";
        for (int i = 0; i < str.length; i++) {
            rstr = rstr + u[Integer.parseInt(str[i] + "")];
        }
        return rstr;
    }

    // 月转化为大写
    public static String monthToUppder(int month) {
        if(month < 10) {
            return numToUpper(month);
        } else if(month == 10){
            return "十";
        } else {
            return "十" + numToUpper(month - 10);
        }
    }

    // 日转化为大写
    public static String dayToUppder(int day) {
        if(day < 20) {
            return monthToUppder(day);
        } else {
            char[] str = String.valueOf(day).toCharArray();
            if(str[1] == '0') {
                return numToUpper(Integer.parseInt(str[0] + "")) + "十";
            }else {
                return numToUpper(Integer.parseInt(str[0] + "")) + "十" + numToUpper(Integer.parseInt(str[1] + ""));
            }
        }
    }
}
